import requests
import simplejson

from ai_quickstart.openai_translator.ai_translator.model import Model


class GLMModel(Model):
    def __init__(self, model_url: str, timeout: int):
        self.model_url = model_url
        self.timeout = timeout

    def make_request(self, prompt):
        try:
            payload = {"prompt": prompt, "history": []}

            response = requests.post(self.model_url, json=payload, timeout=self.timeout)
            response.raise_for_status()
            response_json = response.json()
            translation = response_json["response"]
            return translation, True
        except requests.exceptions.RequestException as e:
            raise Exception(f"请求异常：{e}")
        except requests.exceptions.Timeout as e:
            raise Exception(f"请求超时：{e}")
        except simplejson.errors.JSONDecodeError as e:
            raise Exception(f"JSON解析错误：{e}")
        except Exception as e:
            raise Exception(f"翻译失败：{e}")
        return "", False
